//
//  KKAdviceCommentVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/7.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAdviceCommentVC.h"
#import "KKAdviceComplainVC.h"
#import "HCSStarRatingView.h"
#import "KKComHttp.h"
#import "KKUserInfoDetailM.h"
#import "KKAdditionalAdviceDialog.h"
@interface KKAdviceCommentVC ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet HCSStarRatingView *starTatingView;
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UILabel *company;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (strong,nonatomic) KKUserInfoDetailM *user;
@property (copy,nonatomic) NSString *rankValue;
@property (weak, nonatomic) IBOutlet UITextView *commentContent;

@end

@implementation KKAdviceCommentVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"评论专家";
    self.rankValue=@"0";
    self.commentContent.delegate=self;
    [self.starTatingView addTarget:self action:@selector(selectStar:) forControlEvents:UIControlEventValueChanged];
    [self getAdviceCommentInfo];
}

-(void)initData{
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.user.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    self.nickname.text=self.user.nickname;
    self.company.text=self.user.company;
    self.job.text=self.user.job;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark textview代理
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if([textView.text isEqualToString:@"请输入评论内容"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if(textView.text.length < 1){
        textView.text = @"请输入评论内容";
        textView.textColor = [UIColor colorWithHexString:@"#797778"];
    }
}

#pragma mark 按钮--星星改变
-(void)selectStar:(HCSStarRatingView *)starView{
    int star= (int)starView.value;
    self.rankValue=[NSString stringWithFormat:@"%d",star];
    KKLog(@"%d",star);
}
#pragma mark 按钮--投诉
- (IBAction)complainClick:(id)sender {
    KKAdviceComplainVC *complain=[[KKAdviceComplainVC alloc] init];
    complain.consultId=self.user.consultid;
    [self.navigationController pushViewController:complain animated:YES];
}
#pragma mark 按钮--提交评论
- (IBAction)commitClick:(id)sender {
    if([self.rankValue isEqualToString:@"0"]){
        [self toast:@"请选择评分"];
        return;
    }
    if([self.commentContent.text isEqualToString:@"请输入评论内容"]){
        self.commentContent.text=@"";
    }
    [self commitAdviceComment];
}

#pragma mark 接口--评论用户信息
-(void)getAdviceCommentInfo{
    [self showDialog];
    [KKComHttp getAdviceComplainInfo:self.consultId success:^(id data) {
        self.user=data;
        [self initData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--提交评论
-(void)commitAdviceComment{
    [self showDialog];
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"expertid":self.user.expertid,@"consultid":self.user.consultid,@"judgerank":self.rankValue,@"judgercotent":self.commentContent.text};
    [KKComHttp commitAdviceComment:params success:^(id data) {
        [self dismissDialog];
        self.commentContent.text=@"请输入评论内容";
        if([data isEqualToString:@"error"]){
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            KKAdditionalAdviceDialog *advice=[[[NSBundle mainBundle] loadNibNamed:@"KKAdditionalAdviceDialog" owner:self options:nil] lastObject];
            advice.expertId=self.user.expertid;
            advice.nickname=self.user.nickname;
            advice.company=self.user.company;
            advice.commentVC=self;
            [advice show];
        }
        
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}

@end
